package gui.logado.mensagens.receber;

import conexao.Http;
import conexao.HttpData;
import conexao.HttpListener;
import java.io.DataInputStream;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.lcdui.*;
import recados.Recados;

public class Deletar extends List implements CommandListener, HttpListener {

    private final Command cmdVoltar;
    private final Command cmdConcluido;
    private boolean[] flags;
    private Vector ids;
    private final CaixaEntrada entrada;

    public Deletar() {
        super("Apagar", MULTIPLE);

        this.entrada = Recados.getInstance().getEntrada();

        cmdVoltar = new Command("Voltar", Command.BACK, 0);
        cmdConcluido = new Command("Concluido", Command.OK, 1);

        addCommand(cmdVoltar);
        addCommand(cmdConcluido);

        setCommandListener(this);

        for (int i = 0; i < entrada.size(); i++) {
            append(entrada.getString(i), entrada.getImage(i));
        }

        flags = new boolean[entrada.size()];
        ids = new Vector(1);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdVoltar) {
            Recados.setCurrent(Recados.getInstance().getEntrada());
        } else if (c == cmdConcluido) {
            if (getSelectedFlags(flags) > 0) {
                run();
            } else {
                Recados.setCurrent(new Alert("Ops", "Selecione", null, AlertType.ALARM));
            }
        }
    }

    public void requisicaoConcluida(DataInputStream in) {
        try {
            String st = Http.readLine(in);

            Recados midlet = Recados.getInstance();

            if (st.equals("erro")) {
                String me = Http.readLine(in);
                Recados.setCurrent(new Alert("Erro", me, null, AlertType.ERROR), midlet.getEntrada());
                return;
            }

            Enumeration e = ids.elements();

            while (e.hasMoreElements()) {
                entrada.remove((String) e.nextElement());
            }

            Recados.setCurrent(entrada);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void requisicaoCancelada() {
        Recados.setCurrent(Recados.getInstance().getEntrada());
    }

    public void falhaRequisiçao(String mensagem) {
        Recados.setCurrent(new Alert("Erro", mensagem, null, AlertType.ERROR),Recados.getInstance().getEntrada());
    }

    public void run() {
        Http http = new Http("Deletando");
        HttpData dados = new HttpData();
        http.setHttpData(dados);

        dados.setPagina("/deleta_recado.php");
        http.setListener(this);

        StringBuffer sids = new StringBuffer(1);

        for (int i = 0; i < flags.length; i++) {
            if (flags[i]) {
                String id = entrada.getId(i);
                ids.addElement(id);
                sids.append(id);
                sids.append(",");
            }
        }

        sids.deleteCharAt(sids.length() - 1);

        dados.put("ids", sids.toString());

        Recados ins = Recados.getInstance();

        dados.put("nome", ins.getLoginConfig().getNome());
        dados.put("senha", ins.getLoginConfig().getSenha());

        Recados.setCurrent(http);

        http.doPost(false);
    }
}
